package com.minglead.springboot.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.time.DateFormatUtils;

/**
 * 日期工具类, 继承org.apache.commons.lang.time.DateUtils类
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
	
	private static String[] parsePatterns = {
		"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", 
		"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
		"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

	/**
	 * 得到当前日期字符串 格式（yyyy-MM-dd）
	 */
	public static String getDate() {
		return getDate("yyyy-MM-dd");
	}
	
	/**
	 * 得到当前日期字符串 格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
	 */
	public static String getDate(String pattern) {
		return DateFormatUtils.format(new Date(), pattern);
	}
	
	/**
	 * 得到日期字符串 默认格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
	 */
	public static String formatDate(Date date, Object... pattern) {
		String formatDate = null;
		if (pattern != null && pattern.length > 0) {
			formatDate = DateFormatUtils.format(date, pattern[0].toString());
		} else {
			formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
		}
		return formatDate;
	}
	
	/**
	 * 得到日期时间字符串，转换格式（yyyy-MM-dd HH:mm:ss）
	 */
	public static String formatDateTime(Date date) {
		return formatDate(date, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 得到当前时间字符串 格式（HH:mm:ss）
	 */
	public static String getTime() {
		return formatDate(new Date(), "HH:mm:ss");
	}

	/**
	 * 得到当前日期和时间字符串 格式（yyyy-MM-dd HH:mm:ss）
	 */
	public static String getDateTime() {
		return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 得到当前年份字符串 格式（yyyy）
	 */
	public static String getYear() {
		return formatDate(new Date(), "yyyy");
	}

	public static String getOtherYear(Integer amount){
		amount=amount==null?1:amount;
		Date now=addYears(new Date(), amount);
		return formatDate(now, "yyyy");
	}
	
	/**
	 * 得到当前月份字符串 格式（MM）
	 */
	public static String getMonth() {
		return formatDate(new Date(), "MM");
	}

	/**
	 * 得到当天字符串 格式（dd）
	 */
	public static String getDay() {
		return formatDate(new Date(), "dd");
	}

	/**
	 * 得到当前星期字符串 格式（E）星期几
	 */
	public static String getWeek() {
		return formatDate(new Date(), "E");
	}
	
	/**
	 * 日期型字符串转化为日期 格式
	 * { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", 
	 *   "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm",
	 *   "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm" }
	 */
	public static Date parseDate(Object str) {
		if (str == null){
			return null;
		}
		try {
			return parseDate(str.toString(), parsePatterns);
		} catch (ParseException e) {
			return null;
		}
	}

    
	/**
	 * 字符串转日期
	 * @param strDate 格式：2016-09-09 或者 2016-09-09 00:00:01
	 * @return 返回日期格式
	 */
	public static Date convertStringToDate(String strDate,String datePattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
		try {
		Date date = sdf.parse(strDate);
	    return date;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	
	/**
	 * 获取过去的天数
	 * @param date
	 * @return
	 */
	public static long pastDays(Date date) {
		long t = new Date().getTime()-date.getTime();
		return t/(24*60*60*1000);
	}

	/**
	 * 获取过去的小时
	 * @param date
	 * @return
	 */
	public static long pastHour(Date date) {
		long t = new Date().getTime()-date.getTime();
		return t/(60*60*1000);
	}
	
	/**
	 * 获取过去的分钟
	 * @param date
	 * @return
	 */
	public static long pastMinutes(Date date) {
		long t = new Date().getTime()-date.getTime();
		return t/(60*1000);
	}
	
	/**
	 * 转换为时间（天,时:分:秒.毫秒）
	 * @param timeMillis
	 * @return
	 */
    public static String formatDateTime(long timeMillis){
		long day = timeMillis/(24*60*60*1000);
		long hour = (timeMillis/(60*60*1000)-day*24);
		long min = ((timeMillis/(60*1000))-day*24*60-hour*60);
		long s = (timeMillis/1000-day*24*60*60-hour*60*60-min*60);
		long sss = (timeMillis-day*24*60*60*1000-hour*60*60*1000-min*60*1000-s*1000);
		return (day>0?day+",":"")+hour+":"+min+":"+s+"."+sss;
    }
	
	/**
	 * 获取两个日期之间的天数
	 * 
	 * @param before
	 * @param after
	 * @return
	 */
	public static double getDistanceOfTwoDate(Date before, Date after) {
		long beforeTime = before.getTime();
		long afterTime = after.getTime();
		return (afterTime - beforeTime) / (1000 * 60 * 60 * 24);
	}
	
	/**
	 * 转换中文数字
	 * @return
	 */
	public static String numFormatter(Integer num){
		String res="";
		switch(num){
			case 0:res="〇";break;
			case 1:res="一";break;
			case 2:res="二";break;
			case 3:res="三";break;
			case 4:res="四";break;
			case 5:res="五";break;
			case 6:res="六";break;
			case 7:res="七";break;
			case 8:res="八";break;
			case 9:res="九";break;
		}
		return res;
	}
	
	/**
	 * 日期中文化 如2016-03-01->二〇一六年三月一日
	 * @return
	 */
	public static String dataFormatter(Date source){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		if(source==null)source=new Date();
		String time=sdf.format(source);
		String[] timeStr=time.split("-");
		String res="";
		for(int i=1;i<timeStr[0].length()+1;i++){
			String tempStr=timeStr[0].substring(i-1,i);
			res+=numFormatter(Integer.parseInt(tempStr));
		}
		res+="年"+standerdNumFmt(timeStr[1])+"月"+standerdNumFmt(timeStr[2])+"日";
		return res;
	}
	
	/**
	 * 100以内数字中文化
	 * @param numStr
	 * @return
	 */
	private static String standerdNumFmt(String numStr){
		String res="";
		if(StringUtils.isNotBlank(numStr)){
			if(Integer.parseInt(numStr)>10){
				String tempStr=numStr.substring(0,1);
				if(!"1".equals(tempStr)){
					res+=numFormatter(Integer.parseInt(tempStr));
				}
				res+="十";
				tempStr=numStr.substring(1);
				if(!"0".equals(tempStr)){
					res+=numFormatter(Integer.parseInt(tempStr));
				}
			}else if(Integer.parseInt(numStr)<10){
				res=numFormatter(Integer.parseInt(numStr));
			}else{
				res="十";
			}
		}
		return res;
	}
	
	public static String getDateforTask(String sourceType,String sourceTime){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
   	 	Date date=new Date();
   	 	String starttime=format.format(date);
   	 if("1".equals(sourceType)){
   		 starttime=sourceTime;
   	 }else if("4".equals(sourceType)){
			 starttime=starttime.substring(0, 5)+sourceTime;
	 }else{
   		 if(Integer.parseInt(sourceTime)<10){
   			 if("2".equals(sourceType)){
   				 starttime=starttime.substring(0, 7)+"-0"+sourceTime;
   			 }else if("3".equals(sourceType)){
   				 Calendar cal = Calendar.getInstance();
   				 int month = cal.get(Calendar.MONTH) + 1;
   				 if(month==4||month==7||month==10||month==1){
   					 if(month==10){
   						 starttime=starttime.substring(0, 5)+10+"-0"+sourceTime; 
   					 }else{
   						 starttime=starttime.substring(0, 5)+"0"+month+"-0"+sourceTime; 
   					 }
   					 
   				 }
   			 }
   		 }else{
   			Calendar ca = Calendar.getInstance();
   			ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH)); 
   			int lastDay=ca.get(Calendar.DAY_OF_MONTH);
   			if(Integer.parseInt(sourceTime)>lastDay){
   				sourceTime=""+lastDay;
   			}
   			 if("2".equals(sourceType)){
   				starttime=starttime.substring(0, 7)+"-"+sourceTime;
   			 }else if("3".equals(sourceType)){
   				 Calendar cal = Calendar.getInstance();
   				 int month = cal.get(Calendar.MONTH) + 1;
   				 if(month==4||month==7||month==10||month==1){
   					 if(month==10){
   						 starttime=starttime.substring(0, 5)+10+"-"+sourceTime; 
   					 }else{
   						 starttime=starttime.substring(0, 5)+"0"+month+"-"+sourceTime; 
   					 }
   					 
   				 }
   			 }
   		 }
   		 
   	 }
		return starttime;
	}
	public static String getDateByEndTime(String sourceType,String sourceTime){
		String endTime="";
		if("1".equals(sourceType)){
			endTime=sourceTime;
		}else if("2".equals(sourceType)){
			endTime=sourceTime.substring(0, 4)+"年"+sourceTime.substring(5, 7)+"月";
		}else if("3".equals(sourceType)){
			String year=sourceTime.substring(0, 4)+"年";
			if(sourceTime.substring(5, 7).equals("03")){
				year+="第一季度";
			}else if(sourceTime.substring(5, 7).equals("06")){
				year+="第二季度";
			}else if(sourceTime.substring(5, 7).equals("09")){
				year+="第三季度";
			}else{
				year+="第四季度";
			}
			endTime=year;
		}else{
			endTime=sourceTime.substring(0, 4)+"年";
		}
		
		return endTime;
	}
	
	public static String getDateValue(String sourceType,String sourceTime){
   	 	String starttime="";
	   	 if("1".equals(sourceType)){
	   		 if(sourceTime.equals(getDate())){
	   			starttime=getStartTime("1");
	   		 }else{
	   			 Calendar cal = Calendar.getInstance();
	   			 Date ymd=parseDate(sourceTime);
	   			 cal.setTime(ymd);
				 int iYear = cal.get(Calendar.YEAR);
				 int iMonth = cal.get(Calendar.MONTH)+1;
				 int iDay   = cal.get(Calendar.DAY_OF_MONTH);
		   		 starttime="0 0 0 "+iDay+" "+iMonth+" ? "+iYear+"";
	   		 }
	   		
	   	 }else if("2".equals(sourceType)){
	   		Calendar cal = Calendar.getInstance();
	   		if(sourceTime.equals(""+cal.get(Calendar.DATE))){
	   			starttime=getStartTime("2");
	   		}else{
	   			cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); 
	   			int lastDay=cal.get(Calendar.DAY_OF_MONTH);
	   			if(Integer.parseInt(sourceTime)<=lastDay){
	   				starttime="0 0 0 "+sourceTime+" * ?";
	   			}else{
	   				starttime="0 0 0 "+lastDay+" * ?";
	   			}
	   			
	   		}
	   		
	   	 }else if("3".equals(sourceType)){
	   		if("3".equals(getMonth())||"6".equals(getMonth())||"9".equals(getMonth())||"12".equals(getMonth())){
	   			if(sourceTime.equals(getDay())){
	   				starttime=getStartTime("3");
	   			}
	   		}else{
	   			Calendar cal = Calendar.getInstance();
	   			cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); 
	   			int lastDay=cal.get(Calendar.DAY_OF_MONTH);
	   			if(Integer.parseInt(sourceTime)<=lastDay){
	   				starttime="0 0 0 "+sourceTime+" 3,6,9,12 ? *";
	   			}else{
	   				starttime="0 0 0 "+lastDay+" 3,6,9,12 ? *";
	   			}
	   			
	   		}
	   	 }else{
	   		if((getYear()+"-"+sourceTime).equals(getDate())){
	   			starttime=getStartTime("4");
	   		 }else{
	   			 Calendar cal = Calendar.getInstance();
	   			 Date ymd=parseDate(getYear()+"-"+sourceTime);
	   			 cal.setTime(ymd);
				 int iMonth = cal.get(Calendar.MONTH)+1;
				 int iDay   = cal.get(Calendar.DAY_OF_MONTH);
		   		 starttime="0 0 0 "+iDay+" "+iMonth+" ? * ";
	   		 }
	   	 }
	   		 
			return starttime;
	}
	
	public static String getStartTime(String sourceType){
		String timeStr="";
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MINUTE, 1);
		 int iYear = cal.get(Calendar.YEAR);
		 int iMonth = cal.get(Calendar.MONTH)+1;
		 int iDay   = cal.get(Calendar.DAY_OF_MONTH);
		 int hour=cal.get(Calendar.HOUR_OF_DAY);//小时
		 int minute=cal.get(Calendar.MINUTE);//分 
		 if("1".equals(sourceType)){
			 timeStr="0 "+minute+" "+hour+" "+iDay+" "+iMonth+" ? "+iYear+"";
		 }else if("2".equals(sourceType)){
			 timeStr="0 "+minute+" "+hour+" "+iDay+" * ?";
		 }else if("3".equals(sourceType)){
			 timeStr="0 "+minute+" "+hour+" "+iDay+" 3,6,9,12 ? *";
		 }else{
			 timeStr="0 "+minute+" "+hour+" "+iDay+" "+iMonth+" ? * ";
		 }
		
		return timeStr;
	}
	public static String getEndTime(String defualtEndTime,String delayTime){
		Calendar cal = Calendar.getInstance();
		cal.setTime(parseDate(defualtEndTime));
		cal.add(Calendar.DATE, Integer.parseInt(delayTime));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		String dateStr = sdf.format(cal.getTime());
		return dateStr;
	}
	
	public static String getMonthBatch(String batch) throws ParseException{
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");
        Date dt=sdf.parse(batch);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.MONTH,-1);
        Date dt1=rightNow.getTime();
        String reStr = sdf.format(dt1);
        return reStr;
	}
}
